<?php

namespace App\Http\Controllers\writer;

use App\Model\book;
use App\Model\bookroll;
use App\Model\chapter;
use App\Model\coller;
use Illuminate\Http\Request;


use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

header('Content-type:text/html;charset=utf-8');
class WriterController extends Controller
{

//use App\Http\Controllers\Controller;

//class WriterController extends Controller
//{


    public function book()
    {
        $jg=book::where('wid',session('writerid'))->paginate(4);
//        dd(session(''));

//        return view('linfeng.writer.book',compact('jg'));

        return view('huweize.home.userWorks',compact('jg'));

    }

    public function add()
    {
        $jg=0;
//        return view('linfeng.writer.add',compact('jg'));

        return view('huweize.writer.add',compact('jg'));

    }

    //新增书籍
    public function upadd(Request $request)
    {

          $add=new book();
          $add->bookname=$request->bookname;
          $add->writername=session('nickname');
          $add->cid=$request->type;
            if (!$request->freezhong){
                $add->free=$request->free;
                $add->price=$request->price;
            }else{
                $add->free='all';
            }
          $add->desc=$request->desc;
          $add->addtime=time();
          $add->wid=session('writerid');
          $add->save();
          $id=$add->id;

          if ($id){

            //存放封面图片
          $dir = public_path().'/image/books';

         $request->bookicon->move($dir,$id.'.jpg');

          $upadd=book::find($id);
          $upadd->bookicon=$id.'.jpg';
          $upadd->save();
      }
        return Redirect('huweize/writer/book');
    }

    //显示作品管理页,送入书卷数据与相关的书籍
    public function chapters($id)
    {

        //书id
        $bid=$id;

        //显示所有者个书籍ID查询所有信息
        $jg=0;
        $result=book::find($id);

        //根据书籍id查询出书卷,得到最大书卷
        $bookrolls=bookroll::where('bid',$result->id)->get();
        $mic=array();
        foreach ($bookrolls as $k){
            $mic[]=$k['bookrollssum'];
        }
        $mic=empty($mic)?'':$mic;
        if ($mic){
            //最大的书卷
            $mix=max($mic);
            //根据最大书卷查询最大的卷的id   rid
            $ridjg=bookroll::where('bookrollssum',$mix)->where('bid',$bid)->get();
            foreach ($ridjg as $k){
                $rid=$k['id'];
            }
            $rid;
        }

        //查询出所有的的书卷信息
        $allbookrolls=bookroll::where('bid',$id)->get();


        return view('huweize.writer.chapters',compact('jg','result','bookrolls','mix','rid','allbookrolls','bid'));

//        return view('huweize.writer.chapters',compact('jg','result','bookrolls','mix','rid','allbookrolls','bid'));

    }

    //上传章节正文
    public function addchapters(Request $request)
    {


        //得到ajax传入的数据
        $name=$request->text_one;
        $text=$request->text_two;
        $text=str_replace("\n","<br>",$text);
        $rid=$request->rid;
        $bid=$request->bid;
        $rollssum=$request->mix;
        
        $upadd=book::find($bid);
        $upadd->uptime=time();
        $upadd->save();

        //进行上传新增章节
        $add=new chapter();
        $add->chaptersname=$name;
        $add->rolls=$rollssum;
        $add->addtime=time();
        $add->site=$text;
        $add->bid=$bid;
        $add->rid=$rid;
        $add->save();
        //获取新增的章id
        $newid=$add->id;


        //拼接出的正文存放地址和正文名:书id+卷id+章id,使用file函数存放
//        $dir=base_path().'/text/'.$bid.'_'.$rid.'_'.$newid.'.txt';
//        file_put_contents($dir,$text);
        //得到存放数据库的章节路径
//        $cha='/text/'.$bid.'_'.$rid.'_'.$newid.'.txt';

        //根据当前的书卷id和书卷排行进行查找出,当前的最大章节排行.把新的章节排行加上
        $firs=chapter::where('bid',$bid)->get();
        $firs_mix=array();
        foreach ($firs as $k){
            $firs_mix[]=$k['zhongshu'];
        }
                //把得到的当前属于同一卷同一本书的最大章节加上1,在添加上章节的存放路径
        //把得到的当前属于同一卷同一本书的最大章节加上1,在添加上章节的存放路径

        $dofirs_mix=max($firs_mix);
        $dofirs_mix=$dofirs_mix+1;

        $upadd=chapter::find($newid);
        $upadd->zhongshu=$dofirs_mix;
        $upadd->save();

        //把当前的章节数也在book表加上当前章节和新增时间
        $oldbook=book::find($bid);
        $oldbook->chapterssum=$dofirs_mix;
        $oldbook->uptime=time();
        $oldbook->save();

        return redirect('huweize/writer/book');

    }

    //上传书卷
    public function addbookrolls(Request $request)
    {
        //获取http请求得到上传的数据
        $name=$request->bookrollsname;
        $desc=$request->desc;
        $bid=$request->bid;
        $addtime=time();
        $sum=1;

        //求出最大的目前章数再加上1
        $jg=bookroll::where('bid',$bid)->get();
        $mix=array();
        foreach ($jg as $k){
            $mix[]=$k['bookrollssum'];
        }
        if ($mix){
            $mix=max($mix)+1;
        }else{
            $mix=1;
        }

        //上传到bookrolls数据表
        $add=new bookroll();

       $add->bid=$bid;
       $add->bookrollsname=$name;
       $add->addtime=$addtime;
       $add->desc=$desc;
       $add->bookrollssum=$mix;
       $add->save();
       $id=$add->id;
       return redirect('huweize/writer/book/chapters/'.$bid);

    }

    //已发布章节的查看章节
    public function seechapters(Request $request)
    {

        $sum=$request->sum;
        $rid=$request->rid;
        $new=preg_replace('/\D/s', '', $sum);
//
//
        if (strlen($new)>1){
            $new=substr($new,1);
        }

        $see=chapter::where('rid',$rid)->where('rolls',$new)->get();


        echo json_encode($see);


    }

    //已发布章节的查看正文
    public function text(Request $request)
    {
        $id=$request->id;

        $see=chapter::find($id);


//       $dir=$see->site;

//        $dir=$see->site;
            $text=$see->site;
//        $text=file_get_contents(base_path().$dir);

        echo $text;
    }

    //删除书籍与相对应的卷和章
    public function deletebook($id)
    {
        $jg=book::find($id);

        $result=$jg->delete();
        if ($result){
            unlink(public_path().'/image/books/'.$id.'.jpg');
            DB::table('bookrolls')->where('bid',$id)->delete();
            DB::table('chapters')->where('bid',$id)->delete();
            return redirect('huweize/writer/book');
        }

    }

    //修改书籍信息
    public function updatabook(Request $request,$id)
    {
        if ($request->isMethod('post')){
            $add=book::find($id);
            $add->bookname=$request->bookname;

            if (!$request->freezhong){
                $add->free=$request->free;
                $add->price=$request->price;
            }else{
                $add->free='all';
            }
            $add->desc=$request->desc;
            $add->save();
                if ($request->bookicon){

                unlink(public_path().'/image/books/'.$id.'.jpg');
                //存放封面图片
                $dir = public_path().'/image/books';

                $request->bookicon->move($dir,$id.'.jpg');
             }
            return Redirect('huweize/writer/book');
        }
        $newbook=book::find($id);
        return view('huweize.writer.updatabook',compact('newbook'));
    }



}
